package com.jsz.peini.service.square;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jsz.peini.bean.square.SquareImage;
import com.jsz.peini.bean.square.SquareInfo;
import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.mapper.square.SquareInfoMapper;

@Service
public class SquareInfoService {
	
	@Resource
	private SquareInfoMapper squareInfoMapper;
	
	public HashMap<String,Object> selectLastByUserId(String userId){
		HashMap<String,Object> squareMap = squareInfoMapper.selectLastByUserId(userId);
		return squareMap;
	}

	public int setSquareInfoBySquareInfo(SquareInfo squareInfo){
		int squareInfoNum=squareInfoMapper.setSquareInfoBySquareInfo(squareInfo);
		return squareInfoNum;
	}
	
	public SquareInfo selectSquareInfoIdByUserId(String userId){
		SquareInfo squareInfo=squareInfoMapper.selectSquareInfoIdByUserId(userId);
		return squareInfo;
	}
	
	public int setSquareImageBySquareInfoId(SquareImage squareImage){
		int squareImageNum=squareInfoMapper.setSquareImageBySquareInfoId(squareImage);
		return squareImageNum;
	}
	
	public List<LinkedHashMap<String,Object>> selectsquareImageBySquareInfoId(int squareInfoId){
		List<LinkedHashMap<String,Object>> squareImage=squareInfoMapper.selectsquareImageBySquareInfoId(squareInfoId);
		return squareImage;
	}
	
	@Transactional
	public int deleteSquareAll(Integer squareInfoId){
		int num=0;
		// 查找照片路径
		List<LinkedHashMap<String, Object>> squareImage = selectsquareImageBySquareInfoId(squareInfoId);
		// 删除相关数据多表
		num=deleteSquareImageInfo(squareInfoId);
		num=deleteSquareImageLike(squareInfoId);
		num=deleteSquareImageComment(squareInfoId);
		num=deleteSquareImageImage(squareInfoId);
		// 删除照片
		for (int j = 0; j < squareImage.size(); j++) {
			File file = new File(squareImage.get(j).get("imageSrc").toString());
			if (file.exists()) {
				file.delete();
			}
		}
		return num;
	}
	public int deleteSquareImageInfo(int squareInfoId){
		int deleteNum=squareInfoMapper.deleteSquareImageInfo(squareInfoId);
		return deleteNum;
	}
	public int deleteSquareImageLike(int squareInfoId){
		int deleteNum=squareInfoMapper.deleteSquareImageLike(squareInfoId);
		return deleteNum;
	}
	public int deleteSquareImageComment(int squareInfoId){
		int deleteNum=squareInfoMapper.deleteSquareImageComment(squareInfoId);
		return deleteNum;
	}
	public int deleteSquareImageImage(int squareInfoId){
		int deleteNum=squareInfoMapper.deleteSquareImageImage(squareInfoId);
		return deleteNum;
	}
	
	public List<LinkedHashMap<String,Object>> selectSquareListByUserId(String userId){
		List<LinkedHashMap<String,Object>> squareList = squareInfoMapper.selectSquareListByUserId(userId);
		return squareList;
	}

	public SquareInfo selectByPrimaryKey(Integer squareInfoId) {
		return squareInfoMapper.selectByPrimaryKey(squareInfoId);
	}
}
